package com.ruoyi.report_lq.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.domain.PageQuery;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.ruoyi.report_lq.domain.LqrhdjlbBaDetail;
import com.ruoyi.report_lq.domain.LqydjlbBaDetail;
import com.ruoyi.report_lq.domain.LqzrdjlbBaDetail;
import com.ruoyi.report_lq.domain.bo.LqrhdjlbBaDetailBo;
import com.ruoyi.report_lq.domain.vo.LqrhdjlbBaDetailVo;
import com.ruoyi.report_lq.domain.vo.LqydjlbBaDetailVo;
import com.ruoyi.report_lq.domain.vo.LqzrdjlbBaDetailVo;
import com.ruoyi.report_lq.mapper.LqrhdjlbBaDetailMapper;
import com.ruoyi.report_lq.service.ILqrhdjlbBaDetailService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Collection;

/**
 * 沥青软化点记录Service业务层处理
 *
 * @author zyb
 * @date 2024-06-11
 */
@RequiredArgsConstructor
@Service
public class LqrhdjlbBaDetailServiceImpl implements ILqrhdjlbBaDetailService {

    private final LqrhdjlbBaDetailMapper baseMapper;

    /**
     * 查询沥青软化点记录
     */
    @Override
    public LqrhdjlbBaDetailVo queryById(Long lqrhdid){
        return baseMapper.selectVoById(lqrhdid);
    }

    /**
     *
     * @param infoId
     * @return 针入度集合
     * @method 根据info-id 查询针入度子表的list集合数据
     */
    public List<LqrhdjlbBaDetailVo> queryByInfoId (Long infoId) {
        QueryWrapper<LqrhdjlbBaDetail> query = new QueryWrapper<>();
        query.eq("lqzrdjlbid", infoId);
        return baseMapper.selectVoList(query);
    }

    /**
     * 查询沥青软化点记录列表
     */
    @Override
    public TableDataInfo<LqrhdjlbBaDetailVo> queryPageList(LqrhdjlbBaDetailBo bo, PageQuery pageQuery) {
        LambdaQueryWrapper<LqrhdjlbBaDetail> lqw = buildQueryWrapper(bo);
        Page<LqrhdjlbBaDetailVo> result = baseMapper.selectVoPage(pageQuery.build(), lqw);
        return TableDataInfo.build(result);
    }

    /**
     * 查询沥青软化点记录列表
     */
    @Override
    public List<LqrhdjlbBaDetailVo> queryList(LqrhdjlbBaDetailBo bo) {
        LambdaQueryWrapper<LqrhdjlbBaDetail> lqw = buildQueryWrapper(bo);
        return baseMapper.selectVoList(lqw);
    }

    private LambdaQueryWrapper<LqrhdjlbBaDetail> buildQueryWrapper(LqrhdjlbBaDetailBo bo) {
        Map<String, Object> params = bo.getParams();
        LambdaQueryWrapper<LqrhdjlbBaDetail> lqw = Wrappers.lambdaQuery();
        lqw.eq(bo.getLqzrdjlbid() != null, LqrhdjlbBaDetail::getLqzrdjlbid, bo.getLqzrdjlbid());
        lqw.eq(StringUtils.isNotBlank(bo.getShiybh()), LqrhdjlbBaDetail::getShiybh, bo.getShiybh());
        lqw.eq(StringUtils.isNotBlank(bo.getShaobnytmc()), LqrhdjlbBaDetail::getShaobnytmc, bo.getShaobnytmc());
        lqw.eq(bo.getKais() != null, LqrhdjlbBaDetail::getKais, bo.getKais());
        lqw.eq(bo.getTempv1() != null, LqrhdjlbBaDetail::getTempv1, bo.getTempv1());
        lqw.eq(bo.getTempv2() != null, LqrhdjlbBaDetail::getTempv2, bo.getTempv2());
        lqw.eq(bo.getTempv3() != null, LqrhdjlbBaDetail::getTempv3, bo.getTempv3());
        lqw.eq(bo.getTempv4() != null, LqrhdjlbBaDetail::getTempv4, bo.getTempv4());
        lqw.eq(bo.getTempv5() != null, LqrhdjlbBaDetail::getTempv5, bo.getTempv5());
        lqw.eq(bo.getTempv6() != null, LqrhdjlbBaDetail::getTempv6, bo.getTempv6());
        lqw.eq(bo.getTempv7() != null, LqrhdjlbBaDetail::getTempv7, bo.getTempv7());
        lqw.eq(bo.getTempv8() != null, LqrhdjlbBaDetail::getTempv8, bo.getTempv8());
        lqw.eq(bo.getTempv9() != null, LqrhdjlbBaDetail::getTempv9, bo.getTempv9());
        lqw.eq(bo.getTempv10() != null, LqrhdjlbBaDetail::getTempv10, bo.getTempv10());
        lqw.eq(bo.getTempv11() != null, LqrhdjlbBaDetail::getTempv11, bo.getTempv11());
        lqw.eq(bo.getTempv12() != null, LqrhdjlbBaDetail::getTempv12, bo.getTempv12());
        lqw.eq(bo.getTempv13() != null, LqrhdjlbBaDetail::getTempv13, bo.getTempv13());
        lqw.eq(bo.getTempv14() != null, LqrhdjlbBaDetail::getTempv14, bo.getTempv14());
        lqw.eq(bo.getTempv15() != null, LqrhdjlbBaDetail::getTempv15, bo.getTempv15());
        lqw.eq(bo.getCez() != null, LqrhdjlbBaDetail::getCez, bo.getCez());
        lqw.eq(bo.getPingjz() != null, LqrhdjlbBaDetail::getPingjz, bo.getPingjz());
        return lqw;
    }

    /**
     * 新增沥青软化点记录
     */
    @Override
    public Boolean insertByBo(LqrhdjlbBaDetailBo bo) {
        LqrhdjlbBaDetail add = BeanUtil.toBean(bo, LqrhdjlbBaDetail.class);
        validEntityBeforeSave(add);
        boolean flag = baseMapper.insert(add) > 0;
        if (flag) {
            bo.setExcelId(add.getExcelId());
        }
        return flag;
    }

    /**
     * 修改沥青软化点记录
     */
    @Override
    public Boolean updateByBo(LqrhdjlbBaDetailBo bo) {
        LqrhdjlbBaDetail update = BeanUtil.toBean(bo, LqrhdjlbBaDetail.class);
        validEntityBeforeSave(update);
        return baseMapper.updateById(update) > 0;
    }

    /**
     * 批量新增沥青延度记录子
     */
    public Boolean insertOrUpdateByRhdBo(List<LqrhdjlbBaDetailVo> detailVos, Long pId) {
        detailVos.forEach(detailVo -> {
            LqrhdjlbBaDetail detail = new LqrhdjlbBaDetail();
            detail.setExcelId(detailVo.getExcelId());
            detail.setShiybh(detailVo.getShiybh());
            detail.setShaobnytmc(detailVo.getShaobnytmc());
            detail.setKais(detailVo.getKais());
            detail.setPingjz(detailVo.getPingjz());
            detail.setTempv1(detailVo.getTempv1());
            detail.setTempv2(detailVo.getTempv2());
            detail.setTempv3(detailVo.getTempv3());
            detail.setTempv4(detailVo.getTempv4());
            detail.setTempv5(detailVo.getTempv5());
            detail.setTempv6(detailVo.getTempv6());
            detail.setTempv7(detailVo.getTempv7());
            detail.setTempv8(detailVo.getTempv8());
            detail.setTempv9(detailVo.getTempv9());
            detail.setTempv10(detailVo.getTempv10());
            detail.setTempv11(detailVo.getTempv11());
            detail.setTempv12(detailVo.getTempv12());
            detail.setTempv13(detailVo.getTempv13());
            detail.setTempv14(detailVo.getTempv14());
            detail.setTempv15(detailVo.getTempv15());
            detail.setCez(detailVo.getCez());
            detail.setLqzrdjlbid(pId);
            baseMapper.insertOrUpdate(detail);
        });
        return true;
    }

    /**
     * 保存前的数据校验
     */
    private void validEntityBeforeSave(LqrhdjlbBaDetail entity){
        //TODO 做一些数据校验,如唯一约束
    }

    /**
     * 批量删除沥青软化点记录
     */
    @Override
    public Boolean deleteWithValidByIds(Collection<Long> ids, Boolean isValid) {
        if(isValid){
            //TODO 做一些业务上的校验,判断是否需要校验
        }
        return baseMapper.deleteBatchIds(ids) > 0;
    }
}
